const Service = require('egg').Service;
var cheerio = require('cheerio');
class TaskService extends Service {
    async checkCanGetTask(taskname) {
        var url;
        if (taskname === '抖音') {
            url = "https://cyl.blgpay.com/?mid=1"
        } else if (taskname === '快手') {
            url = "https://cyl.blgpay.com/?mid=2"
        } else if (taskname === '火山') {
            url = "https://cyl.blgpay.com/?mid=3"
        }
        return new Promise((resolve, reject) => {
            try {
                this.ctx.service.util.httpReq(url, 'get', 'utf-8', function (body) {
                    var $ = cheerio.load(body);
                    var canGetTaskArray = []
                    $(".IndexAppList li").each(function (index, item) {
                        canGetTaskArray.push({
                            id: $(item).attr("date-id"),
                            remark: $(item).find(".detailbox .line1 span").text(),
                            price: $(item).find(".rtbox .price span").text(),
                            status: $(item).find(".rtbox a").text(),
                            media_name: taskname
                        })
                    })
                    console.log('canGetTaskArray')
                    resolve(canGetTaskArray.filter(function (item) {
                        return item.status === "接单"
                    }))
                })
            } catch (err) {
                reject(err)
            }
        })
    }
    // 领取任务
    async getTack(id) {
        var result = await this.ctx.curl('https://cyl.blgpay.com/?c=Task&a=getTask', {
            method: 'POST',
            dataType: 'json',
            heasers: {
                "Cookie": this.ctx.app.config.Cookie,
                "User-Agent": this.ctx.app.config.userAgent
            },
            data: {
                token: this.ctx.app.config.token,
                item_id: id
            },
            timeout: 3000,
        });
        if (result.data.code === '1') {
            return id
        } else {
            return false
        }
    }
    // 上传图片
    async uploadImg(base64) {
        var result = await this.ctx.curl('https://cyl.blgpay.com/?a=imgUpload', {
            method: 'POST',
            dataType: 'json',
            heasers: {
                "Cookie": this.ctx.app.config.Cookie,
                "User-Agent": this.ctx.app.config.userAgent
            },
            data: {
                token: this.ctx.app.config.token,
                imgdata: base64
            },
            timeout: 10000,
        });
        if (result.data.code === '1') {
            return result.data
        } else {
            return false
        }
    }

    // 上传图片
    async goTask(id, banners, remark) {
        var result = await this.ctx.curl('https://cyl.blgpay.com/?c=Task&a=task_update', {
            method: 'POST',
            dataType: 'json',
            heasers: {
                "Cookie": this.ctx.app.config.Cookie,
                "User-Agent": this.ctx.app.config.userAgent
            },
            data: {
                token: this.ctx.app.config.token,
                id: id,
                "banners[]": banners,
                remark: remark
            },
            timeout: 3000,
        });
        if (result.data.code === '1') {
            return result.data
        } else {
            return false
        }
    }

    // 存入数据库
    async makeTaskData() {
        var result = await this.ctx.curl('https://cyl.blgpay.com/?c=Task&a=tasklog_list', {
            method: 'POST',
            dataType: 'json',
            heasers: {
                "Cookie": this.ctx.app.config.Cookie,
                "User-Agent": this.ctx.app.config.userAgent
            },
            data: {
                token: this.ctx.app.config.token,
                status: '1',
                page: '1'
            },
            timeout: 3000,
        });
        var taskArray = result.data.data.list
        // 存入数据库
        if (taskArray.length > 0) {
            taskArray.forEach(async item => {
                try {
                    console.log(item)
                    await this.app.mysql.insert('task', {
                        id: item.id,
                        task_id: item.task_id,
                        price: item.price,
                        task_name: item.task_name,
                        video_url: item.video_url,
                        media_name: item.media_name,
                        level_name: item.level_name,
                        end_time: item.end_time
                    });
                } catch (err) {
                    console.log("插入数据错误")
                }
            })
        } else {
            console.log("没有查到数据")
        }
        return taskArray
    }


}

module.exports = TaskService;